<!DOCTYPE html>
<html lang="zh-cn">
   <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>{$CONF['mallTitle']}后台管理中心</title>
      <link href="__PUBLIC__/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
      <link href="__CSS__/AdminLTE.css" rel="stylesheet" type="text/css" />
      <script src="__PUBLIC__/js/jquery.min.js"></script>
      <script src="__PUBLIC__/plugins/bootstrap/js/bootstrap.min.js"></script>
      <script src="__PUBLIC__/js/common.js"></script>
      <script src="__PUBLIC__/plugins/plugins/plugins.js"></script>
   </head>
   <script>

   </script>
   <body class='wst-page'>
      <style>
    .isShow{cursor:pointer;width:80px; padding:5px;display:block;}
       .cur{cursor:pointer;}
       .showHide{background:#f00;color:#fff;cursor:pointer;width:20px;display:inline-block; margin-right:10px;}
       </style>
       <div class='wst-tbar' style='text-align:right;height:25px;'>
       <a class="btn btn-success glyphicon glyphicon-plus" href="{:U('Admin/Posts/toAddCate')}" style='float:right'>新增</a>
       </div>
       <div class="wst-body"> 
        <table class="table table-hover table-striped table-bordered wst-list">
           <thead>
             <tr>
               <th>分类名称</th>
               <th>排序号</th>
               <th>是否显示(双击修改)</th>
               <th>操作</th>
             </tr>
           </thead>
           <tbody id="list">
             <volist name="info" id="io">
               <tr id="{$io.cid}" class="tr" >
                 <td><input  value="{$io.name}" data-id="{$io.cid}"  class="cateName lis_5 td_id" data-pid="0"  type="text" name="name" placeholder="请填写分类名称"  style="width:80%;"  /></td>
                 <td><input value="{$io.sort}" data-id="{$io.cid}"   type="text" class="sort lis_5 td_id" name="sort"    style="width:50px; " /></td>
                 <td><span class="lis_{$io.cid} td_id isShow" data-id="{$io.cid}" data-pid="0"><if condition="$io['isShow']">是<else/>否</if></span></td>
                 <td><span  class= "btn btn-danger cur delCate td_id"  data-id="{$io.cid}" data-pid="0"   >删除</span></td>
               </tr>
             </volist>
           </tbody>
        </table>
   </body>
   <script>
   //添加父节点
   $('body').on('click','#addParentCate',function(){
    var html="<tr id='0' class='tr'> ";    
      html+=$('#demo').html();
    html+='</tr>';
    var obj=$(html);
    obj.attr('id',0);
    obj.find('.td_id').attr('data-id',0);
    obj.find('.td_id').attr('data-pid',0);
      $('#list').append(obj);
   })
   
   //添加子节点
      $('body').on('click','.addCate',function(){
      var append=$(this).parent().parent();
      if(append.attr('id')==0){
        layer.msg('完成一级分类后才能添加子分类');
        return;
      }
      var pid=$(this).attr('data-id');
    var html="<tr id='0' class='tr"+pid+"'>";    
      html+=$('#demo').html();
    html+='</tr>';
    var obj=$(html);
    obj.attr('id',0);
    obj.find('.td_id').attr('data-id',0);
    obj.find('.showHide').text('>').css('background','none');
    obj.find('.showHide').css('color','#666');
    obj.find('.td_id').attr('data-pid',pid);
    obj.find('.addCate').remove(); 
    append.after(obj);
   })

   //删除分类
   $('body').on('click','.delCate',function(){
     var id=$(this).attr('data-id');
     var pid=$(this).attr('data-pid');
     var _this=$(this);
      Plugins.confirm({title:'信息提示',content:'您确定要删除该分类吗?',okText:'确定',cancelText:'取消',okFun:function(){
      Plugins.closeWindow();
      Plugins.waitTips({title:'信息提示',content:'正在操作，请稍后...'});
      $.ajax({
          type: "POST",
          url: "{:U('Posts/delCate')}",
          data: {
              pid:pid,
              id:id
          },
          dataType: "json",
          success: function(data){
            if(data.status==0){
              Plugins.setWaitTipsMsg({content:'操作成功',timeout:1000,callback:function(){
                if(pid==0){
                   $(_this).parent().parent().remove();
                   $('.tr'+id).remove();
                 }else{
                   $(_this).parent().parent().remove();
                 }
              }});
            }else{
              Plugins.closeWindow();
              Plugins.Tips({title:'信息提示',icon:'error',content:'操作失败!',timeout:1000});
            }
          }
      });
       }});
     
     })
   //收起同节点
   $('body').on('click','.showHide',function(){
     var pid=$(this).attr('data-id');
     var status=   $(this).attr('data-status');
     if(status==1){
       $('.tr'+pid).hide();
       $(this).text('+');
       $(this).attr('data-status',0);
     }else{
       $(this).text('-');
       $(this).attr('data-status',1);
       $('.tr'+pid).show();
     }
   })
     
   //修改分类名称
   $('body').on('blur','.cateName',function(){
    var _this=$(this);
    var pid=$(this).attr('data-pid');
    var id=$(this).attr('data-id');
    val=$(this).val();
   if(val.length<=0 ||val==''){
     return;
   }
   Plugins.waitTips({title:'信息提示',content:'正在操作，请稍后...'});
    $.ajax({
          type: "POST",
          url: "{:U('Posts/addCate')}",
          data: {
              pid:pid,
              id:id,
              val:val
          },
          dataType: "json",
          success: function(data){
              try{
              if(data.status>0){
                var ele= $(_this).parent().parent();
                ele.attr('id',data.status);
                ele.find('.showHide').attr('data-id',data.status);
                ele.find('.td_id').attr('data-id',data.status);
                Plugins.setWaitTipsMsg({content:'修改成功',timeout:1000,callback:function(){
                        location.reload();
                    }});
              }else if(data.status==0){
                Plugins.setWaitTipsMsg({content:'修改成功',timeout:1000,callback:function(){
                        location.reload();
                    }});
              }else{
                Plugins.closeWindow();
                Plugins.Tips({title:'信息提示',icon:'error',content:'未修改或操作失败!',timeout:1000});
              }
              }catch(err){
              }
          }
      });
   })
   
   //排序
     $('body').on('blur','.sort',function(){
    //type新增，1修改
    var id=$(this).attr('data-id');
    var val=$(this).val();
   if(val.length<0 ||val==''||isNaN(val)){
     alert(2);
     layer.msg('请输入数字');
     return;
   }
    Plugins.waitTips({title:'信息提示',content:'正在操作，请稍后...'});
    $.ajax({
          type: "POST",
          url: "{:U('Posts/sortHandle')}",
          data: {
              id:id,
              val:val
          },
          dataType: "json",
          success: function(data){
              try{
              if(data.status!=-1){
                Plugins.setWaitTipsMsg({content:'修改成功，请刷新',timeout:1000,callback:function(){
                        location.reload(true);
                    }});
              }else{
                Plugins.closeWindow();
                Plugins.Tips({title:'信息提示',icon:'error',content:'未修改或操作失败!',timeout:1000});
              }
              }catch(err){
                
              }
          }
      });
   
   });
 
   $('body').on('dblclick','.isShow',function(){
     var id=$(this).attr('data-id');
     var pid=$(this).attr('data-pid');
     var show=0;
     if($(this).text()=='是'){
       show=0;
     }else{
       show=1;
     }
     var _this=$(this);
      Plugins.waitTips({title:'信息提示',content:'正在操作，请稍后...'});
      $.ajax({
          type: "POST",
          url: "{:U('Posts/isShow')}",
          data: {
              pid:pid,
              id:id,
              show:show
          },
          dataType: "json",
          success: function(data){
            if(data.status==0){
                Plugins.setWaitTipsMsg({content:'设置成功',timeout:1000,callback:function(){
                        //location.reload(true);
                    }});
             if(pid==0){
               if(show==0){
                 $('.lis_'+id).text('否');
               }else{
                 $('.lis_'+id).text('是');
               }
             }else{
               if(show==0){
                 $(_this).text('否');
               }else{
                 $(_this).text('是');
               } 
             }
            }else{
                Plugins.closeWindow();
                Plugins.Tips({title:'信息提示',icon:'error',content:'设置失败!',timeout:1000});
            }
          }
      });
     
   })
   
   </script>
</html>